package com.ocom.accesservice.controller;

import com.ocom.accesservice.service.MjDeviceInfoService;
import com.ocom.accesservice.service.TimeZoneService;
import com.ocom.accesservice.service.ZoneDateService;
import com.ocom.common.constant.AccessServiceUrl;
import com.ocom.common.entity.access.EntityMjTimeZone;
import com.ocom.common.entity.access.EntityMjZoneDate;
import com.ocom.common.enums.ResultEnum;
import com.ocom.common.request.web.GetZoneDateListRequest;
import com.ocom.common.resp.Result;
import com.ocom.common.response.web.GetZoneDateListResponse;
import com.ocom.common.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@RestController
public class WebTimeZoneController {

    @Autowired
    private TimeZoneService timeZoneService;

    @Autowired
    private ZoneDateService zoneDateService;


    @Autowired
    private MjDeviceInfoService mjDeviceInfoService;


    /*
     * @Description:获取时区列表
     * @Author: lu 
     * @Date: 2022/1/13 9:29
     * @param companyId:  单位编号
     * @return: com.ocom.common.resp.Result
     **/
    @PostMapping(AccessServiceUrl.URL_getZoneList)
    public Result getCountByComId(Long companyId){
        List<Map<String,Object>> list=timeZoneService.selectGetZoneList(companyId);
        if (list!=null){
            return ResultUtil.success(list);
        }
        return ResultUtil.error(ResultEnum.Err_1001);
    }

    /*
     * @Description:获取时区设置列表
     * @Author: lu 
     * @Date: 2022/1/13 15:16
     * @param getZoneDateListRequest:  
     * @return: com.ocom.common.resp.Result
     **/
    @PostMapping(AccessServiceUrl.URL_getZoneDateList)
    public Result getCountByComId(@Valid @RequestBody GetZoneDateListRequest getZoneDateListRequest){
        //判断是否要分页,0表示不分页
        List<GetZoneDateListResponse> list=new ArrayList<>();
        if (getZoneDateListRequest.getPage()==null || getZoneDateListRequest.getPage()==0){
            List<EntityMjZoneDate> zonedDateTimeList=zoneDateService.selectZonedDateTimeList(getZoneDateListRequest.getComId());
            if (zonedDateTimeList.size()>0){
                for (EntityMjZoneDate entityMjZoneDate : zonedDateTimeList) {
                    if(entityMjZoneDate.getZoneNo()!=null){
                        EntityMjTimeZone zoneName=timeZoneService.selectZoneName(entityMjZoneDate.getZoneNo(),getZoneDateListRequest.getComId());
                        if (zoneName==null || zoneName.getZoneName()==null){
                            return ResultUtil.error(ResultEnum.Err_1001);
                        }else {
                            GetZoneDateListResponse getZoneDateListResponse=new GetZoneDateListResponse();
                            getZoneDateListResponse.setDate(entityMjZoneDate.getDate());
                            getZoneDateListResponse.setZoneNo(entityMjZoneDate.getZoneNo().intValue());
                            getZoneDateListResponse.setDayDate(entityMjZoneDate.getDayDate().intValue());
                            getZoneDateListResponse.setZoneName(zoneName.getZoneName());
                            list.add(getZoneDateListResponse);
                        }
                    }

                }
            }else {
                return ResultUtil.error(ResultEnum.Err_1001);
            }
        }else {
            //分页
            List<EntityMjZoneDate> zonedDateTimeList=zoneDateService.selectZonedDateTimeListPage(getZoneDateListRequest,getZoneDateListRequest.getComId());
            if (zonedDateTimeList.size()>0){
                for (EntityMjZoneDate entityMjZoneDate : zonedDateTimeList) {
                    EntityMjTimeZone zoneName=timeZoneService.selectZoneName(entityMjZoneDate.getZoneNo(),getZoneDateListRequest.getComId());
                    if (zoneName==null || zoneName.getZoneName()==null){
                        return ResultUtil.error(ResultEnum.Err_1001);
                    }else {
                        GetZoneDateListResponse getZoneDateListResponse=new GetZoneDateListResponse();
                        getZoneDateListResponse.setDate(entityMjZoneDate.getDate());
                        getZoneDateListResponse.setZoneNo(entityMjZoneDate.getZoneNo().intValue());
                        getZoneDateListResponse.setDayDate(entityMjZoneDate.getDayDate().intValue());
                        getZoneDateListResponse.setZoneName(zoneName.getZoneName());
                        list.add(getZoneDateListResponse);
                    }
                }
            }else {
                return ResultUtil.error(ResultEnum.Err_1001);
            }

        }
        return ResultUtil.success(list);
    }

    /*
     * @Description:设置日期时区
     * @Author: lu 
     * @Date: 2022/1/14 11:04
     * @param entityMjZoneDate:  
     * @return: com.ocom.common.resp.Result
     **/
    @PostMapping(AccessServiceUrl.URL_setDateZone)
    public Result setDateZone(@Valid @RequestBody EntityMjZoneDate entityMjZoneDate){

        Integer i=zoneDateService.setDateZone(entityMjZoneDate,entityMjZoneDate.getComId());
        if (i>0){
            mjDeviceInfoService.updateZoneVer(entityMjZoneDate.getComId());

            return ResultUtil.success();
        }
        return ResultUtil.error(ResultEnum.Err_2016);
    }

    /*
     * @Description:删除日期时区
     * @Author: lu 
     * @Date: 2022/1/14 16:38
     * @param entityMjZoneDate:  
     * @return: com.ocom.common.resp.Result
     **/
    @PostMapping(AccessServiceUrl.URL_removeDateZone)
    public Result removeDateZone(@Valid @RequestBody EntityMjZoneDate entityMjZoneDate){

        Integer i=zoneDateService.removeDateZone(entityMjZoneDate,entityMjZoneDate.getComId());
        if (i==null){
            return ResultUtil.error(ResultEnum.Err_1001);
        }

        mjDeviceInfoService.updateZoneVer(entityMjZoneDate.getComId());

        return ResultUtil.success();
    }

}
